﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Irony.Parsing;

namespace Flit
{
    static class Extensions
    {
        internal static IEnumerable<ParseTreeNode> DescendantsWithTermType(this ParseTreeNode node, Func<FlitGrammar, BnfTerm> desiredTermTypeSelector)
        {
            FlitGrammar g = (FlitGrammar)node.Term.Grammar;
            BnfTerm term = desiredTermTypeSelector(g);
            return node.Descendants().Where(x => x.Term == term);
        }

        internal static IEnumerable<ParseTreeNode> Descendants(this ParseTreeNode node)
        {
            foreach (var c in node.ChildNodes)
            {
                yield return c;
                foreach (var gc in c.Descendants())
                {
                    yield return gc;
                }
            }
        }

        internal static string InvariantFormat(this string s, params object[] args)
        {
            return string.Format(CultureInfo.InvariantCulture, s, args);
        }
    }
}
